package org.laobai.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.laobai.common.asserts.Asserts;
import org.laobai.common.exception.BPException;
import org.laobai.common.struct.PageResult;
import org.laobai.common.struct.Pair;
import org.laobai.user.entity.User;
import org.laobai.user.mapper.UserMapper;
import org.laobai.user.service.UserService;
import org.laobai.user.util.StringUtils;
import org.laobai.user.vo.SearchVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiusan
 * @since 2020-11-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    private final static ObjectMapper MAPPER = new ObjectMapper();

    @Override
    public PageResult<User> page(Integer currPage, Integer pageSize, SearchVO vo) {
        // 分页
        Page<User> userPage = new Page<>(currPage, pageSize);

        // 条件查询
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        if (Objects.nonNull(vo)) {
            String keyWord = vo.getKeyWord();
            if (StringUtils.isNotEmpty(keyWord)) {
                wrapper.like("name", keyWord);
            }
        }
        userPage = page(userPage, wrapper);
        return PageResult.of(userPage.getRecords(), userPage.getTotal());
    }

    @Override
    public boolean saveUser(User user) {
        return save(user);
    }

    @Override
    public boolean updateUser(User user) {
        return updateById(user);
    }

    @Override
    public boolean delById(Long id) {
        return removeById(id);
    }

    @Override
    public boolean delByIds(List<Long> ids) {
        return removeByIds(ids);
    }

    @Override
    public Pair<Boolean,String> login(String name, String pwd) {
        //根据name查询
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        List<User> list = list(wrapper);

        //是否查到，查不到返回
        Asserts.isNotNull(list,"用户名或者密码错误");

        if(list.size()>1){
            throw new BPException(500,"数据有误，name"+name+"重复");
        }

        //加密pwd
        pwd = pwd+"123252135";
        //判断加密pwd 和数据库pwd
        User userFromDB = list.get(0);
        if(pwd.equals(userFromDB.getPwd())){
            //登录成功
//            UserInfo userInfo = new UserInfo();
//            userInfo.setId(userFromDB.getId());
//            userInfo.setName(userFromDB.getName());

//            String jwt = JwtUtil.createJWT(userFromDB.getId().toString(), JsonUtils.toString(userInfo), null);
//            return Pair.of(true,jwt);
        }
        // 登录失败
        return Pair.of(false,null);
    }

    @Override
    public List<User> getUserByName(String name) {


        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        List<User> list = list(wrapper);
        return  list;
    }
}
